/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.FaceDesign;
import com.yami.shop.dao.FaceDesignMapper;
import com.yami.shop.service.FaceDesignService;
import com.yami.shop.common.util.PageParam;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设计稿服务实现
 * @author AI Assistant
 */
@Service
@AllArgsConstructor
public class FaceDesignServiceImpl extends ServiceImpl<FaceDesignMapper, FaceDesign> implements FaceDesignService {

    private final FaceDesignMapper faceDesignMapper;

    @Override
    public List<FaceDesign> listByUserIdAndStatus(String userId, Integer status) {
        LambdaQueryWrapper<FaceDesign> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FaceDesign::getUserId, userId);
        if (status != null) {
            queryWrapper.eq(FaceDesign::getDesignStatus, status);
        }
        queryWrapper.orderByDesc(FaceDesign::getCreateTime);
        return faceDesignMapper.selectList(queryWrapper);
    }

    @Override
    public Integer countByUserIdAndStatus(String userId, Integer status) {
        LambdaQueryWrapper<FaceDesign> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FaceDesign::getUserId, userId);
        if (status != null) {
            queryWrapper.eq(FaceDesign::getDesignStatus, status);
        }
        return Math.toIntExact(faceDesignMapper.selectCount(queryWrapper));
    }

    @Override
    public FaceDesign getByDraftId(Long draftId) {
        LambdaQueryWrapper<FaceDesign> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FaceDesign::getDraftId, draftId);
        return faceDesignMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateStatus(Long designId, Integer status) {
        FaceDesign faceDesign = new FaceDesign();
        faceDesign.setDesignId(designId);
        faceDesign.setDesignStatus(status);
        faceDesignMapper.updateById(faceDesign);
    }

    @Override
    public void saveFaceDesign(FaceDesign faceDesign) {
        faceDesignMapper.insert(faceDesign);
    }

    @Override
    public void updateFaceDesign(FaceDesign faceDesign) {
        faceDesignMapper.updateById(faceDesign);
    }

    @Override
    public void deleteFaceDesign(Long designId) {
        faceDesignMapper.deleteById(designId);
    }

    @Override
    public IPage<FaceDesign> pageByUserIdAndStatus(PageParam<FaceDesign> page, String userId, Integer status) {
        return faceDesignMapper.selectPage(page, new LambdaQueryWrapper<FaceDesign>()
                .eq(userId != null, FaceDesign::getUserId, userId)
                .eq(status != null, FaceDesign::getDesignStatus, status)
                .orderByDesc(FaceDesign::getCreateTime));
    }
}
